layui.define(function(exports){
	SortMap = function(length){
		this.length = length || 20;
		this.data = {}
		this.keys = new Array();
	}
	
	Array.prototype.indexOf = function(val) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == val) return i;
		}
		return -1;
	};
	
	Array.prototype.remove = function(val) {
		var index = this.indexOf(val);
		if (index > -1) {
			this.splice(index, 1);
		}
	};
	
	SortMap.prototype.add = function(key ,o){
		if(this.data[key]){
			this.keys.remove(key);
			this.data[key] = o;
		}else{
			if(this.keys.length >= this.length){
				this.pop()
			}
			this.keys.push(key);
			this.data[key] = o;
		}
	}
	
	SortMap.prototype.pop = function(){
		if(this.keys.length <= 0){
			return;
		}
		var key = this.keys.pop();
		delete this.data[key]
	}
	
	SortMap.prototype.get = function(key){
		return this.data[key];
	};
	
	SortMap.prototype.size = function(){
		return this.keys.length;
	}
	
	SortMap.prototype.isEmpty = function () {
		return this.keys.length == 0;
	};
	
	SortMap.prototype.remove = function (key) {
		this.keys.remove(key);
		delete this.data[key]
	};
 
 
 	// 遍历Map,执行处理函数. 回调函数 function(key,value,index){..}
 	SortMap.prototype.each = function(fn){   
 		if(typeof fn != 'function'){   
 			return;   
 		}   
 		var len = this.size();
 		for(var i = 0; i < len; i++){   
 			var k = this.keys[i];   
 			fn(k, this.data[k], i);   
 		}   
 	};
 	
 	// 获取键值数组,返回键值对象{key,value}的数组
 	SortMap.prototype.entrys = function() {     
 		var len = this.size();     
 		var entrys = new Array(len);     
 		for (var i = 0; i < len; i++) {     
 			entrys[i] = {     
 				key : this.keys[i],     
 				value : this.data[i]     
 			};     
 		}
 		return entrys;     
 	}; 
 	
 	exports("sortMap", {
 		create : function(length){
 			return new SortMap(length);
 		}
 	});
 });